import types from './types'
import * as esQuery from './utils/esSearch'

const mutations = {
  // 更新上一次打来的日志类别
  [types.SET_LAST_LOGTYPE] (state, data) {
    state.lastLogType = data
  },
  // 更新监测日志类别
  [types.SET_LOGTYPE] (state, data) {
    state.logType = data
  },
  // 设置等待
  [types.SET_LODING] (state, key) {
    state.loadingCtrl[key] = !state.loadingCtrl[key]
  },
  // 更新时间选择器时间
  [types.SET_DAYRANGE] (state, data) {
    state.dayRange = data
  },
  // 生成对应es搜索语句
  [types.SET_SEARCHINPUT_ES] (state, data) {
    esQuery.setEsQuery(data)
  },
  // 更新输入框内数据
  [types.SET_SEARCHINPUT_TEXT] (state, data) {
    state.searchInput = data
  },
  // 更新搜索历史
  [types.SET_HISTORY] (state, data) {
    state.history = data
  },
  // 更新标签聚合图表
  [types.SET_GROUP_PIC] (state, data) {
    state.groupPicData = data
  },
  // 更新标签聚合详情es语句
  [types.SET_SEARCH_LINE] (state, data) {
    state.searchLine = data
  },
  // 清空筛选条件选择状态
  [types.CLEAR_FILTER] (state) {
    state.choose = []
    for (let i = 0; i < state.filterCategory.length; i++) {
      state.choose.push([])
    }
    let answer = []
    for (let i = 0; i < state.filterCategory.length; i++) {
      let ans = state.filterCategory[i].esSearch(state.choose[i])
      if (Object.prototype.toString.call(ans) === '[object Array]') {
        // 表示此类别没有选择过滤向
      } else {
        if (ans !== null) {
          answer.push(ans)
        }
      }
    }
    esQuery.setEsFilter(answer)
  },
  // 更新筛选条件选择状态
  [types.SET_GROUP_CHOOSE] (state, data) {
    state.choose = data
    let filter = []
    for (let idx in state.filterCategory) {
      let item = {}
      item = state.filterCategory[idx].esSearch(state.choose[idx])
      if (Object.prototype.toString.call(item) === '[object Array]') {
      // 说明是空数组，没有过滤条件
      } else {
        filter.push(item)
      }
    }
    esQuery.setEsFilter(filter)
  },
  // 更新搜索结果
  [types.SET_SEARCH] (state, data) {
    state.tableData = data.hits.hits
    state.pagination.total = data.hits.total
  },
  // 更新标签聚合数据
  [types.SET_LABELGROUP] (state, data) {
    // 列表数据
    state.tableData = data.hits.hits
    state.pagination.total = data.hits.total
  },
  // 更新es搜索时间
  [types.SET_TIMERANGE] (state, data) {
    state.timeRange = data
  },
  // 控制“更多”按钮渲染
  [types.SET_MORE_VISIABLE] (state, data) {
    state.filterCategory[data.index].moreVisibleCtrl = data.value
  },
  // 控制聚合详情展示
  [types.SET_GROUPDETAIL_VISIABLE] (state, index) {
    if (state.groupCategory[index].detailVisibleCtrl === false) {
      // 关闭所有
      for (let el of state.groupCategory) {
        el.detailVisibleCtrl = false
      }
      state.groupCategory[index].detailVisibleCtrl = true
    } else {
      state.groupCategory[index].detailVisibleCtrl = false
    }
  },
  // 更新详情页原生数据
  [types.SET_RAWDATA] (state, data) {
    state.rawData = data
  },
  // 更新详情页 监测信息数据
  [types.SET_DETAIL_INFO] (state, data) {
    state.detailInfo = data
  },
  // 更新详情页处理后数据
  [types.SET_HANDLEDATA] (state, data) {
    state.handleData = data
  },
  // 每页条数
  [types.SET_CURRENTSIZE] (state, data) {
    esQuery.setPage(state.pagination.currentPage, data)
    state.pagination.pageSize = data
  },
  // 当前页码
  [types.SET_CURRENTPAGE] (state, data) {
    esQuery.setPage(data, state.pagination.pageSize)
    state.pagination.currentPage = data
  },
  // 筛选条件 省市区级联列表
  [types.SET_LOCATION] (state, value) {
    // 去除None值
    for (var i = 0; i < value.length; i++) {
      if (value[i].children.length >= 1) {
        for (var j = 0; j < value[i].children.length; j++) {
          if (value[i].children[j].value == null) {
            value[i].children.splice(j, 1)
          }
          if (value[i].children[j] !== undefined) {
            if (value[i].children[j].children.length >= 1) {
              for (var k = 0; k < value[i].children[j].children.length; k++) {
                if (value[i].children[j].children[k].value == null) {
                  value[i].children[j].children.splice(k, 1)
                }
              }
            }
          }
        }
      }
    }
    state.location = value
  },
  // 更新筛选条件 日志类型（中、英文)
  [types.SET_STYPE_DICT] (state, data) {
    state.stypeDict = data
  },
  // 更新筛选条件列表
  [types.SET_FILTER_GROUP] (state, data) {
    state.filterData = data
  },
  // 更新'更多'按钮 穿梭框数据
  [types.SET_TRANSFER_LEFT] (state, data) {
    state.transferLeft = data
  },
  // 等多的弹出框 更新filterData中某一类的值 data是一个数组
  [types.SET_FILTER_DATA_ITEM] (state, data) {
    state.filterData[data[1]] = data[0]
  },
  // 更新筛选条件列表(副本)
  [types.SET_FILTER_GROUP_COPY] (state, data) {
    state.filterDataCopy = data
  }
}

export default mutations
